package com.lagou.edu.homeworkthymeleaf.Controller;

import com.lagou.edu.homeworkthymeleaf.pojo.Article;
import com.lagou.edu.homeworkthymeleaf.service.QueryArticle;
import com.lagou.edu.homeworkthymeleaf.service.impl.QueryArticleImpl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;


@Controller
public class HomeWorkController {

    @Autowired
    private QueryArticle queryArticle;

    @RequestMapping("test1")
    public String testHomework(ModelMap modelMap){
        Article article = queryArticle.queryArticle(1);
        Integer integer = queryArticle.selectCount();

        modelMap.put("article",article);
        modelMap.put("nextPage",article.getId()+1);
        modelMap.put("previousPage",article.getId()-1);
        modelMap.put("totalPage",integer);
        return "index";

    }

    @RequestMapping("test2")
    public String testHomework(ModelMap modelMap, @RequestParam int current){
        Article article = queryArticle.queryArticle(current);
        Integer integer = queryArticle.selectCount();
        Integer nextPage = article.getId()+1;
        Integer previousPage = article.getId()-1;
        if(nextPage > integer){
            nextPage = integer;
        }
        if(previousPage < 1){
            previousPage = 1;
        }
        modelMap.put("article",article);
        modelMap.put("nextPage",nextPage);
        modelMap.put("previousPage",previousPage);
        modelMap.put("totalPage",integer);
        return "index";

    }
}
